<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>冒泡排序</title>

    <script>

        function bubbleSort(arr){

            for(let i=0; i<arr.length-1; i++){

                for(let j=i+1; j<arr.length; j++){

                    if(arr[j] < arr[i]){
                        swap(arr, i, j)
                    }
                
                }

            }

            return arr;
        }

        function swap(arr, i, j){
            let a = arr[i];
            arr[i] = arr[j];
            arr[j] = a;
        }

        const arr = [7, 5, 6, 2, 3, 1, 9, 8, 4]

        console.log(bubbleSort(arr));
        
        

    </script>
</head>

<body>

</body>

</html>